
import os, sys


def walk(list_recv, path=".\\", exclude=None, ext_list=['.h', '.c']):
    list = os.listdir(path)
    for p in list:
        pathname = os.path.join(path,p)
        if pathname in exclude:
            continue
        if not os.path.isfile(pathname):
            walk(list_recv, pathname, exclude, ext_list)
        else:
            _, ext = os.path.splitext(pathname)
            if ext in ext_list:
                # print pathname
                list_recv.append(pathname)
            
if __name__ == "__main__":
    if os.path.exists("dir_list.txt"):
        f = open("dir_list.txt", "r")
        strings = f.readlines()
        f.close()
        
        dir_list = []
        for i in strings:
            dir_list.append(i[:-1])
            
            
        path = ".\\"
        
        # get all files to list_all
        list_all = []
        walk(list_all, path, dir_list)
        i = 1;
        list_file_name = "list_all"
        if not os.path.exists(list_file_name+".txt"):
            list_file_name = list_file_name+".txt"
        else:
            while(os.path.exists(list_file_name+str(i)+".txt")):
                i += 1
            list_file_name = list_file_name+str(i)+".txt"
        f_all = open(list_file_name, "w")
        print "open file " +  list_file_name
        for i in list_all:
            f_all.write(i+"\n")
        f_all.close()
        print "close file " +  list_file_name
        
        print "Run ctags..."
        os.system("ctags -L "+list_file_name)
        
        print "Run gtags..."
        os.system("gtags -f "+list_file_name)
        
        # os.remove(list_file_name)
        # print "File " + list_file_name + " removed"
        print "Finish"
        
        